﻿using ChongJu.Camera.MvCameraControl;
using ChongJu.EventManager.Rating;
using ChongJu.GlobalInfo;
using ChongJu.ModelManager.Camera;
using Public.Logger;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ChongJu.Camera
{
    public class Camera
    {
        private static readonly object _obj = new object();
        private static Camera _instance;
        public static Camera Instance
        {
            get
            {
                if (_instance == null)
                    lock (_obj)
                        if (_instance == null)
                            _instance = new Camera();

                return _instance;
            }
        }

        private ICameraBase _camera;

        public event Action<Bitmap, DateTime> ImageCallBackEvent;

        public Camera()
        {
            _camera = new MvCamera();
            _camera.ImageCallBackEvent += _camera_ImageCallBackEvent;
        }

        private void _camera_ImageCallBackEvent(Bitmap obj, DateTime tm)
        {
            ImageCallBackEvent?.Invoke(obj, tm);
        }

        public bool Init(CloudService.CloudService cloud)
        {
            try
            {
                var cfg = CameraConfig.CameraCfg;
                if (cfg == null)
                    return false;

                var bol = _camera.Init(cfg, cloud);
                return bol;
            }
            catch (Exception ex)
            {
                ConnectStatusEvent.OnConnect(ConnectType.Camera, false);
                Logger.Error("初始化相机异常", ex);
                return false;
            };
        }

        public bool Open()
        {
            try
            {
                var status = _camera.Open();
                ConnectStatusEvent.OnConnect(ConnectType.Camera, status);
                return status;
            }
            catch (Exception ex)
            {
                ConnectStatusEvent.OnConnect(ConnectType.Camera, false);
                Logger.Error("打开相机异常");
                return false;
            };
        }

        public bool Close()
        {
            try
            {
                return _camera.Close();
            }
            catch (Exception ex)
            {
                Logger.Error("关闭相机异常");
                return false;
            };
        }

        public void Start()
        {
            _camera.Start();
        }

        public void Stop()
        {
            _camera.Stop();
        }

        public Task<string> TakePicture(int idx = 0)
        {
            return Task.Factory.StartNew(() =>
            {
                try
                {
                    _camera.SetTriggerBySoftware();
                    _camera.TriggerBySoftware();
                    _camera.SetTriggerByLine(idx);

                    return null;
                }
                catch (Exception ex) 
                { 
                    return ex.Message; 
                }
            });
        }

        public void TakeOne()
        {
            //_camera.SetTriggerBySoftware();
            _camera.TriggerBySoftware();
            //_camera.SetTriggerByLine(0);
        }

        public void SetParams(CameraModel cfg)
        {
            SetParams(cfg, false);
        }

        public void SetParams(CameraModel cfg, bool setWidthAndHeight)
        {
            _camera.SetParams(cfg, setWidthAndHeight);
        }

        public string SetCalibrationCfg(CameraModel cfg)
        {
            return _camera.SetCalibrationParms(cfg);
        }

        public void SetTriggerBySoftware()
        {
            _camera.SetTriggerBySoftware();
        }

        public void SetTriggerByLine0()
        {
            _camera.SetTriggerByLine();
        }

        public bool OpneLine1CloseLine2()
        {
            return _camera.OpenLine1CloseLine2();
        }

        public bool OpneLine2CloseLine1()
        {
            return _camera.OpenLine2CloseLine1();
        }
    }
}
